package com.zrrd.tree;

import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class Tree {
    public static class Node {
        Object data;
        List<Node> children;

        public Node(Object data) {
            this.data = data;
        }
    }

    public void levelOrderTraversal(Tree.Node node) {
        Queue<Tree.Node> nodeQueue = new LinkedList<>();
        nodeQueue.offer(node);
        while (!nodeQueue.isEmpty()) {
            Tree.Node node1 = nodeQueue.poll();
            System.out.println(node1.data);
            if (node1.children != null && node1.children.size() > 0) {
                for (Node child : node1.children)
                    nodeQueue.offer(child);
            }
        }
    }

    public void firstOrderTraversal(Tree.Node root) {
        System.out.println(root.data);
        if (root.children != null && root.children.size() > 0) {
            for (Node child : root.children) {
                firstOrderTraversal(child);
            }
        }
    }

    public void lastOrderTraversal(Tree.Node root) {
        if (root.children != null && root.children.size() > 0) {
            for (Node child : root.children) {
                firstOrderTraversal(child);
            }
        }
        System.out.println(root.data);
    }
}
